<?php

namespace App\Http\Controllers\Api\V1;

//use Illuminate\Http\Request;
use App\Http\Requests\Api\ApiRequest as Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Interfaces\CartService;
use App\Interfaces\OrderService;
use App\Exceptions\Interfaces\OrderServiceException;
use App\Exceptions\Interfaces\OrderGoodsServiceException;
use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;

use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\User;
use App\Models\RefundGoods;
use App\Models\Address;

class OrderController extends Controller
{

	/**
     * @api {post} /order/order_confirm 订单确认页面
     * @apiName Order/order_confirm
     * @apiGroup Order
     * @apiDescription 订单确认页面.
     *
     * @apiParam {String[]} cartGoods  商品id及购买数量的数组，如：cartGoods = array(0=>array('goods_id'=>1346, 'buy_num'=>1), 1=>array('goods_id'=>1343, 'buy_num'=>1)).
     * @apiParam {String} from  是否是直接从商品页面购买，是的话为：detail，从购物车过来为空字符串
     * @apiParam {Number} [address_id]  用户收货地址id
     * @apiParam {Number} [e_coupon_id]      当前选中的e贸易商品的用户优惠券id
     * @apiParam {Number} [c_coupon_id]      当前选中的一般贸易商品的用户优惠券id
     *
     * @apiSuccess {Object[]}  Object      返回订单确认页面信息
     * @apiSuccess {Object[]}  Object.buyGoods      		要购买的商品信息
     * @apiSuccess {String[]}  Object.buyGoods      		要购买的商品id和对应的购买数量，如："1342": "1"
     * @apiSuccess {String}  Object.total_tariff_fee      	关税
     * @apiSuccess {String}  Object.total_buy_number      	商品总数
     * @apiSuccess {String}  Object.total_shipping_fee      运费
     * @apiSuccess {String}  Object.total_order_price      	总价格
     * @apiSuccess {String}  Object.total_pay_price         支付价格
     * @apiSuccess {String}  Object.total_coupon_price           优惠券优惠金额
     * @apiSuccess {String}  Object.total_discount_price         限时折扣优惠金额
     * @apiSuccess {Object[]}  Object.orders      			订单分单信息，e贸易商品和一般商品
     * @apiSuccess {Object[]}  Object.orders.eshopGoods      	要购买的商品信息
     * @apiSuccess {Object[]}  Object.orders.eshopGoods.orderGoods      		要购买的商品信息
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.goods_id      			 商品id
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.cat_id      			 分类id
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.goods_sn                  商品编号（编码）
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.goods_bar_code      			 商品条形码
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.goods_name     		 商品名称
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.goods_pet_name      	 商品副标题
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.goods_desc      		 商品简介
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.brand_id      			 品牌id
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.goods_number   		 商品数量
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.goods_weight   		 商品重量
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.market_price   		 商品市场价格
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.shop_price     		 商品商城价格
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.keywords      			 商品关键字
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.goods_content  		 商品内容
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.goods_thumb    		 商品图片id
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.resource             商品来源
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.goods_gallery_id        商品相册id
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.is_on_sale              商品是否上架，1：上架；2：下架
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.sort_order              商品排序
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.is_delete               商品是否已被删除（目前不用）
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.is_home_recommend       商品是否首页推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.is_cat_recommend        商品是否分类推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.shop_id                 商品所属商铺id
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.sales_num               商品销售数量
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.deleted_at                商品删除时间（默认为空，如果不为空则该商品已被删除）
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.search_keywords         商品搜索关键词
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.created_at              商品创建时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.updated_at              商品编辑/更新时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.audit_status            商品核审状态：0：未审核；1：审核通过；2：审核不通过
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.cat_name      			 分类id
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.cat_desc      			 分类描述
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.parent_id      		 父分类id
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.measure_unit      		 计量单位
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.goods_type      		 分类搜索属性
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.tariff_percent          关税比例
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.excise_duty_percent          消费税
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.value_added_tax_percent          增值税
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.is_recommend              否是隐藏：0：隐藏；1：显示
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.cat_picture              分类展示图片
     * @apiSuccess {Object[]}  Object.orders.eshopGoods.orderGoods.discount             商品可以使用最大折扣信息
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.discount_id             折扣id
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.discount.discount_name             折扣名称
     * @apiSuccess {String}  Object.orders.eshopGoods.orderGoods.discount.discount_label             折扣标签
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.discount_type             折扣类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.discount_value             折扣百分比，1-99
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.limit_amount             每位用户限用商品数量，0：不限；大于0表示限用个数
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.start_time             折扣开始时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.end_time               折扣结束时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.deleted_at             折扣删除时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.created_at             折扣创建时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.discount.updated_at             折扣更新时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.goods_real_price              商品折扣后的单价
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.goods_discount_price          商品折扣后的总价
     * @apiSuccess {Number}  Object.orders.eshopGoods.orderGoods.mobile_goods_thumb      		 手机端商品详情
     * @apiSuccess {Object[]}  Object.orders.eshopGoods.coupons                     可用的优惠券
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.id                    用户优惠券表主键id
     * @apiSuccess {String}  Object.orders.eshopGoods.coupons.name                  优惠券名称
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.value                 优惠券金额
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.amount                优惠券发放数量
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.receive_amount        优惠券已经被领取的数量
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.limit_fir             优惠券使用规则1，0：无限制；大于0：满多少使用
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.type                  优惠券可用类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {String}  Object.orders.eshopGoods.coupons.remarks               优惠券备注说明
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.created_at            优惠券创建时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.start_time            优惠券开始时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.end_time              优惠券过期时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.updated_at            优惠券更新时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.deleted_at            优惠券删除时间
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.user_id               用户id
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.coupon_id             优惠券表id
     * @apiSuccess {Number}  Object.orders.eshopGoods.coupons.use_time              用户使用时间
     * @apiSuccess {Object[]}  Object.orders.caculator      	分类（普通或e贸易）商品的计算信息
     * @apiSuccess {Number}  Object.orders.caculator.total_number      	商品总数
     * @apiSuccess {Number}  Object.orders.caculator.total_item      	商品种类数量
     * @apiSuccess {Number}  Object.orders.caculator.total_price      	商品总价
     * @apiSuccess {Number}  Object.orders.caculator.total_tariff_fee      	税费
     * @apiSuccess {Number}  Object.orders.caculator.shipping_fee      	运费
     * @apiSuccess {Number}  Object.orders.caculator.pay_price      	支付价格
     * @apiSuccess {String}  Object.orders.type         订单类型，0：普通善品；1：e贸易商品
     * @apiSuccess {String}  Object.from         直接购买为：detail,否则为空字符串
     * @apiSuccess {String}  Object.current_coupon_id       当前已选中的优惠券id
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单创建成功
     * {
	 *   "buyGoods": {
	 *     "1342": "1",
	 *     "1346": "2"
	 *   },
	 *   "total_tariff_fee": 0,
	 *   "total_buy_number": 3,
	 *   "total_shipping_fee": 0,
	 *   "total_order_price": 220,
	 *   "total_pay_price": 220,
      *   "total_coupon_price": 0,
      *   "total_discount_price": 0,
	 *   "orders": {
	 *     "eshopGoods": {
	 *       "orderGoods": [
	 *         {
	 *           "goods_id": 1342,
	 *           "cat_id": 393,
	 *           "goods_sn": "aa",
      *           "goods_bar_code": "8809392416039",
	 *           "goods_name": "净白霜",
	 *           "goods_pet_name": "aa",
	 *           "goods_desc": "aa",
	 *           "brand_id": 77,
	 *           "goods_number": 10,
	 *           "goods_weight": "0.000",
	 *           "market_price": "22.00",
	 *           "shop_price": "22.00",
	 *           "keywords": "BB霜,CC霜",
	 *           "goods_content": "",
	 *           "goods_thumb": "",
	 *           "resource": "aa",
	 *           "type": 1,
	 *           "goods_gallery_id": 0,
	 *           "is_on_sale": 1,
	 *           "add_time": 0,
	 *           "sort_order": 100,
	 *           "is_delete": 0,
	 *           "is_home_recommend": 0,
	 *           "is_cat_recommend": 0,
	 *           "shop_id": 1,
	 *           "sales_num": 0,
	 *           "deleted_at": null,
	 *           "search_keywords": "22,HANSKIN,美妆个护,美容彩妆,彩妆,彩妆,化妆品,BB霜/CC霜,BB霜,CC霜",
	 *           "created_at": "2016-02-18 15:44:44",
	 *           "updated_at": "2016-04-08 09:33:28",
	 *           "audit_status": 1,
	 *           "goods_bar_code": "",
	 *           "cat_name": "BB霜/CC霜",
	 *           "cat_desc": "3级分类BB霜/CC霜",
	 *           "parent_id": 392,
	 *           "measure_unit": "盒",
	 *           "goods_type": 0,
	 *           "tariff_percent": 50,
     *           "excise_duty_percent": 0,
     *           "value_added_tax_percent": 17,
	 *           "is_recommend": 1,
     *           "cat_picture": null,
     *           "discount": {
     *             "discount_id": 3,
     *             "discount_name": "9折大酬宾(全场通用)",
     *             "discount_label": "9折大酬宾哈",
     *             "discount_type": 1,
     *             "discount_value": 90,
     *             "limit_amount": 100,
     *             "start_time": "2016-07-22 11:00:55",
     *             "end_time": "2016-07-31 00:00:00",
     *             "deleted_at": null,
     *             "created_at": "2016-05-19 10:26:39",
     *             "updated_at": "2016-07-22 11:00:55"
     *           },
     *           "goods_real_price": "19.80",
     *           "goods_discount_price": 19.80,
     *           "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/038fc726480df8ae2fa03617b4cb8c03.png"
	 *         }
	 *       ],
     *       "coupons": [
     *         {
     *           "id": 52,
     *           "name": "全场通用优惠券",
     *           "value": 10,
     *           "amount": 50,
     *           "receive_amount": 11,
     *           "limit_fir": 100,
     *           "type": 1,
     *           "remarks": "测试优惠券",
     *           "created_at": "2016-07-14 10:57:17",
     *           "start_time": "2016-04-26 00:00:00",
     *           "end_time": "2016-07-15 00:00:00",
     *           "updated_at": "2016-07-14 10:57:17",
     *           "deleted_at": null,
     *           "user_id": 59,
     *           "coupon_id": 1,
     *           "use_time": null
     *         }
     *       ],
	 *       "caculator": {
	 *         "total_number": 1,
	 *         "total_item": 1,
	 *         "total_price": "22.00",
	 *         "total_tariff_fee": 0,
	 *         "shipping_fee": 0,
	 *         "pay_price": 22
	 *       },
	 *       "type": 1
	 *     },
	 *     "cshopGoods": {
	 *       "orderGoods": [
	 *         {
	 *           "goods_id": 1346,
	 *           "cat_id": 398,
	 *           "goods_sn": "010101",
	 *           "goods_name": "莲花香",
	 *           "goods_pet_name": "莲花香牌男士香水",
	 *           "goods_desc": "莲花香牌男士香水",
	 *           "brand_id": 181,
	 *           "goods_number": 102,
	 *           "goods_weight": "0.000",
	 *           "market_price": "199.00",
	 *           "shop_price": "99.00",
	 *           "keywords": "香水,女士",
	 *           "goods_content": "",
	 *           "goods_thumb": "ca17d3071b6fcde0eb90e717469a5b68",
	 *           "resource": "中国",
	 *           "type": 0,
	 *           "goods_gallery_id": 0,
	 *           "is_on_sale": 1,
	 *           "add_time": 0,
	 *           "sort_order": 100,
	 *           "is_delete": 0,
	 *           "is_home_recommend": 0,
	 *           "is_cat_recommend": 0,
	 *           "shop_id": 5,
	 *           "sales_num": 0,
	 *           "deleted_at": null,
	 *           "search_keywords": ",维多利亚的秘密,美妆个护,美容彩妆,香水/香氛,香水,香氛,女士香水,香水,女士",
	 *           "warehouse_id": 0,
	 *           "created_at": "2016-04-01 14:13:29",
	 *           "updated_at": "2016-04-12 16:08:35",
	 *           "audit_status": 1,
	 *           "goods_bar_code": "010101",
	 *           "cat_name": "女士香水",
	 *           "cat_desc": "3级分类女士香水",
	 *           "parent_id": 397,
	 *           "measure_unit": "盒",
	 *           "goods_type": 0,
	 *           "tariff_percent": 50,
	 *           "is_recommend": 1,
     *           "cat_picture": null,
     *           "discount": {
     *             "discount_id": 3,
     *             "discount_name": "9折大酬宾(全场通用)",
     *             "discount_label": "9折大酬宾哈",
     *             "discount_type": 1,
     *             "discount_value": 90,
     *             "limit_amount": 100,
     *             "start_time": "2016-07-22 11:00:55",
     *             "end_time": "2016-07-31 00:00:00",
     *             "deleted_at": null,
     *             "created_at": "2016-05-19 10:26:39",
     *             "updated_at": "2016-07-22 11:00:55"
     *           },
     *           "goods_real_price": "89.10",
     *           "goods_discount_price": 178.20,
     *           "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/75053874b1a36e0ee118ba73d0a26947.png"
	 *         }
	 *       ],
     *       "coupons": [],
	 *       "caculator": {
	 *         "total_number": 2,
	 *         "total_item": 1,
	 *         "total_price": "198.00",
	 *         "total_tariff_fee": 0,
	 *         "shipping_fee": 0,
	 *         "pay_price": 198
	 *       },
	 *       "type": 0
	 *     }
	 *   },
     *   "from": "detail",
     *   "current_coupon_id": ""
	 * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单创建失败
     * {
	 *   "message": "用户id不能为空"
	 * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单创建失败
	 * {
	 *   "message": "购买商品不能为空"
	 * }
	 */
	public function orderConfirm(Request $request, OrderService $orderService)
	{

          $user_id = $request->apiUser->get()->id;

          $cartGoods = $request->cartGoods;
          $address_id = $request->address_id;

          if (empty($user_id)) {

               throw new ApiInputErrorException("用户id不能为空");
          }//end if()

          if (empty($cartGoods)) {

               throw new ApiInputErrorException("购买商品不能为空");
          }//end if()

          if ($address_id) {

               $address = Address::find($address_id);

               if (empty($address)) {

                    $address_id = 0;
               }
          }//end if()

          $current_coupon_discount=['coupon_id'=>'', 'discount_id'=>''];

          $coupons=[];

          $current_coupon_id = '';

          //优惠券
          if ($request->get('e_coupon_id')) {

               $coupons['e_coupon_id'] = $request->get('e_coupon_id');

               $current_coupon_id = $request->get('e_coupon_id');
          }

          if ($request->get('c_coupon_id')) {

               $coupons['c_coupon_id'] = $request->get('c_coupon_id');

               $current_coupon_id = $request->get('c_coupon_id');
          }


          try {

               // $ordersInfo = $orderService->getBuyGoodsInfo($cartGoods, $address_id, [], [], [], $user_id);
               // $ordersInfo = $orderService->getBuyGoodsInfo($cartGoods, $address_id, $coupons, $discounts, $current_discount_arr, $user_id);
               $ordersInfo = $orderService->getBuyGoodsInfo($cartGoods, $address_id, $coupons, $user_id);

               $ordersInfo['from'] = $request->from ? $request->from : '';

               $ordersInfo['current_coupon_id'] = $current_coupon_id;

               return response()->json($ordersInfo);

          } catch (OrderServiceException $e) {

               throw new ApiDoneErrorException($e->getMessage());
          }

	}//end func orderConfirm()



	/**
	 *
     * @api {post} /order/create_order 创建订单
     * @apiName Order/create_order
     * @apiGroup Order
     * @apiDescription 创建订单.
     * @apiParam {String[]} goods  商品id及购买数量的数组，如：goods = array(0=>array('goods_id'=>1346, 'buy_num'=>1), 1=>array('goods_id'=>1343, 'buy_num'=>1)).
     * @apiParam {Number} address_id  收货地址id.
     * @apiParam {Number} from  是否是直接购买商品，from=detail表示是，留空或者不存在表示从购物车结算.
     * @apiParam {Number} [is_all_goods_exception]  是否抛出所有商品的异常信息，0：不抛出所有的商品异常，只抛出第一个商品的第一个异常；1：抛出所有的商品异常.
     * @apiParam {Number} [e_coupon_id]      e贸易订单要使用的用户优惠券id.
     * @apiParam {Number} [c_coupon_id]      一般贸易订单要使用的用户优惠券id.
     *
     * @apiSuccess {Object[]}  Object      返回创建的订单id数组
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单创建成功
     * [
     *   141,
     *   142
     * ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单创建失败
     * {
	 *   "message": "用户id不能为空"
	 * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单创建失败
	 * {
	 *   "message": "购买商品不能为空"
	 * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //订单创建失败
	 * {
	 *   "message": "收货地址不能为空"
	 * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 202
     * //订单创建失败
	 * {
	 *   "message": "商品雅漾库存不足"
	 * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 202
     * //订单创建失败
	 * {
	 *   "message": "商品雅漾已下架"
	 * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 202
     * //订单创建失败
	 * {
	 *   "message": "下单失败"
	 * }
     * @apiErrorExample Error-Response 07:
     * HTTP/1.1 400
     * //订单创建失败
     * {
     *   "message": "当前用户类型不能为空"
     * }
     * @apiErrorExample Error-Response 08:
     * HTTP/1.1 400
     * //订单创建失败
     * {
     *   "message": "当前用户类型值不能为空"
     * }
     * @apiErrorExample Error-Response 09:
     * HTTP/1.1 202
     * //订单创建失败
     * {
     *   "message": "type参数类型错误,必须为SESSION或USER"
     * }
     * @apiErrorExample Error-Response 10:
     * HTTP/1.1 202
     * //订单创建失败
     * {
     *   "message": "部分商品已下架或库存不足",
     *   "data": {
     *     "not_on_sale": [
     *       503
     *     ],
     *     "not_goods_number": [
     *       483,
     *       503,
     *       966,
     *       967
     *     ]
     *   }
     * }
     * @apiErrorExample Error-Response 11:
     * HTTP/1.1 400
     * //订单创建失败
     * {
     *   "message": "收货地址不存在"
     * }
	 */
    public function createOrder(Request $request, OrderService $orderService,CartService $cartService)
    {
          $user_id = $request->apiUser->get()->id;
          $goods = $request->goods;
          $address_id = $request->address_id;
          $is_all_goods_exception = isset($request->is_all_goods_exception) ? $request->is_all_goods_exception : 0 ;

          if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
          }//end if()

          if (empty($goods)) {

            throw new ApiInputErrorException("购买商品不能为空");
          }//end if()

          if (empty($address_id)) {

            throw new ApiInputErrorException("收货地址不能为空");
          }else{

               $address = Address::find($address_id);

               if (empty($address)) {

                    throw new ApiInputErrorException("收货地址不存在");
               }
          }//end if()

          //优惠券
          $coupons = [
            'e_coupon_id'=>$request->get('e_coupon_id'),
            'c_coupon_id'=>$request->get('c_coupon_id')
          ];

          try {

          	$order_ids = $orderService->createOrder($user_id, $goods, $address_id, $coupons, 1, $is_all_goods_exception);

          	if (!empty($order_ids)) {

                 $return = $order_ids;
          	}//end if()

            //清空购物车所选商品
            if (empty($request->from)){

                $cartService->init('USER', $user_id);

                $cartService->removeCartGoods($goods);
            }

            return response()->json($return);
          } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());

          } catch (OrderGoodsServiceException $e){

            throw new ApiDoneErrorException($e->getMessage(), $e->getData());
          }

    }//end func createOrder()

	/**
     * @api {post} /order/get_orders_by_condations 根据订单条件查询订单
     * @apiName Order/get_orders_by_condations
     * @apiGroup Order
     * @apiDescription 根据订单条件查询订单.
     *
     * @apiParam {Number} user_id  用户id.
     * @apiParam {String[]} [condations]  查询用户订单的条件，为空默认查询该用户的所有订单；order_status：订单状态，1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消；comment_status：评论状态，0：未评论；1：已评论；date：时间，1：近三个月；2：近一年；3：当年；4：去年；5：去年以前；refund_goods：不为空则是查询退货订单；condations = array('order_status'=>1, 'comment_status'=>1, 'date'=>1, 'refund_goods'=>1).
     * @apiParam {Number} [page]  每页多少条数据，可为空，默认10条.
     * @apiParam {Number} [current_page]  当前页数，可为空，默认为第一页.
	 *
	 *
     * @apiSuccess {Object}  Object        查询到的商品信息
     * @apiSuccess {number}  Object.per_page      每页多少条数据
     * @apiSuccess {number}  Object.current_page  当前页数
     * @apiSuccess {number}  Object.last_page     最后一页的页数
     * @apiSuccess {String}  Object.next_page_url 下一页的url地址
     * @apiSuccess {String}  Object.prev_page_url 上一页的url地址
     * @apiSuccess {number}  Object.from          从第几条数据开始查询
     * @apiSuccess {number}  Object.to            查询到第几条数据
     * @apiSuccess {Object[data]}  Object.data    符合条件的订单数组
     * @apiSuccess {number}  Object.data.order_id      订单id
     * @apiSuccess {String}  Object.data.order_sn      订单编号
     * @apiSuccess {number}  Object.data.user_id       用户id
     * @apiSuccess {number}  Object.data.order_status  订单状态：1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消；
     * @apiSuccess {number}  Object.data.order_type    订单类型，0：一般商品；1：e贸易商品
     * @apiSuccess {number}  Object.data.shop_id         店铺id
     * @apiSuccess {number}  Object.data.order_price         订单价格
     * @apiSuccess {number}  Object.data.order_tariff_fee         订单关税价格
     * @apiSuccess {number}  Object.data.shipping_fee         邮费
     * @apiSuccess {number}  Object.data.total_goods_number         商品总数
     * @apiSuccess {number}  Object.data.pay_price         支付价格
     * @apiSuccess {String}  Object.data.country         国家
     * @apiSuccess {String}  Object.data.province        省份
     * @apiSuccess {String}  Object.data.city            城市
     * @apiSuccess {String}  Object.data.district        区
     * @apiSuccess {String}  Object.data.address         详细地地址
     * @apiSuccess {String}  Object.data.consignee       收货人
     * @apiSuccess {String}  Object.data.id_type          证件类型：默认身份证（1：身份证）
     * @apiSuccess {String}  Object.data.id_card         身份证号码
     * @apiSuccess {String}  Object.data.invoice_message         用户发票信息
     * @apiSuccess {String}  Object.data.order_message         订单备注信息
     * @apiSuccess {String}  Object.data.deleted_at         删除时间
     * @apiSuccess {String}  Object.data.add_time         下单时间
     * @apiSuccess {String}  Object.data.shipping_type         配送方式
     * @apiSuccess {String}  Object.data.shipping_num         运单号
     * @apiSuccess {String}  Object.data.shipping_time         发货时间
     * @apiSuccess {String}  Object.data.shipping_message         用户自定义发货信息
     * @apiSuccess {number}  Object.data.order_pay_id         支付ID 关联order_pay 主键
     * @apiSuccess {number}  Object.data.push_tarrif         是否已经推关，0：未推关；1：已推关
     * @apiSuccess {String}  Object.data.push_tarrif_time         推关时间
     * @apiSuccess {String}  Object.data.recive_goods_time         收货时间
     * @apiSuccess {String}  Object.data.cancel_reason         取消订单原因
     * @apiSuccess {String}  Object.data.cancel_time         取消订单时间
     * @apiSuccess {number}  Object.data.is_remind         是否提醒发货：0：不提醒；1：提醒
     * @apiSuccess {number}  Object.data.pay_push         支付推关结果：1、成功；2、失败
     * @apiSuccess {number}  Object.data.pay_push_success_time         支付推关成功的时间
     * @apiSuccess {String}  Object.data.user_name         用户名
     * @apiSuccess {number}  Object.data.comment_status         评论状态
     * @apiSuccess {number}  Object.data.goods_id         商品id
     * @apiSuccess {Object[]}  Object.data.order_goods         订单商品信息
     * @apiSuccess {number}  Object.data.order_goods.goods_id         商品id
     * @apiSuccess {Number}  Object.data.order_goods.goods_id                商品id
     * @apiSuccess {Number}  Object.data.order_goods.cat_id                  分类id
     * @apiSuccess {String}  Object.data.order_goods.goods_sn                商品编号
     * @apiSuccess {String}  Object.data.order_goods.goods_name              商品名称
     * @apiSuccess {String}  Object.data.order_goods.goods_pet_name          商品副标题
     * @apiSuccess {String}  Object.data.order_goods.goods_desc              商品简介
     * @apiSuccess {Number}  Object.data.order_goods.brand_id                品牌id
     * @apiSuccess {Number}  Object.data.order_goods.goods_number            商品数量
     * @apiSuccess {Number}  Object.data.order_goods.goods_weight            商品重量
     * @apiSuccess {Number}  Object.data.order_goods.market_price            商品市场价格
     * @apiSuccess {Number}  Object.data.order_goods.shop_price              商品商城价格
     * @apiSuccess {String}  Object.data.order_goods.keywords                商品关键词
     * @apiSuccess {String}  Object.data.order_goods.goods_content           商品内容
     * @apiSuccess {String}  Object.data.order_goods.goods_thumb             商品缩略图
     * @apiSuccess {String}  Object.data.order_goods.resource                商品来源
     * @apiSuccess {Number}  Object.data.order_goods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.data.order_goods.goods_gallery_id        商品相册id
     * @apiSuccess {Number}  Object.data.order_goods.is_on_sale              商品是否上架，1：上架；2：下架
     * @apiSuccess {Number}  Object.data.order_goods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.data.order_goods.sort_order              商品排序
     * @apiSuccess {Number}  Object.data.order_goods.is_delete               商品是否已被删除（目前不用）
     * @apiSuccess {Number}  Object.data.order_goods.is_home_recommend       商品是否首页推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.data.order_goods.is_cat_recommend        商品是否分类推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.data.order_goods.shop_id                 商品所属商铺id
     * @apiSuccess {Number}  Object.data.order_goods.sales_num               商品销售数量
     * @apiSuccess {String}  Object.data.order_goods.deleted_at                商品删除时间（默认为空，如果不为空则该商品已被删除）
     * @apiSuccess {Number}  Object.data.order_goods.search_keywords         商品搜索关键词
     * @apiSuccess {Number}  Object.data.order_goods.warehouse_id            商品所在仓库id（暂不可用）
     * @apiSuccess {Number}  Object.data.order_goods.created_at              商品创建时间
     * @apiSuccess {Number}  Object.data.order_goods.updated_at              商品编辑/更新时间
     * @apiSuccess {Number}  Object.data.order_goods.audit_status            商品核审状态：0：未审核；1：审核通过；2：审核不通过
     * @apiSuccess {Number}  Object.data.order_goods.buy_number              订单中购买的该商品数量
     * @apiSuccess {Number}  Object.data.order_goods.order_goods_id          订单商品表id
     * @apiSuccess {Number}  Object.data.order_goods.comment_status          订单商品评论状态，0：未评论；1：已评论
     * @apiSuccess {Number}  Object.data.order_goods.goods_bar_code          商品条形码
     * @apiSuccess {String}  Object.data.order_goods.mobile_goods_thumb      商品图片完整地址
     * @apiSuccess {Object[]}  Object      订单创建成功的时候返回创建的订单id数组
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单查询成功
	 * {
	 *     "total": 39,
	 *     "per_page": "3",
	 *     "current_page": 1,
	 *     "last_page": 13,
	 *     "next_page_url": "http://afuyigou.app/api/order/get_rders_by_condations?=2",
	 *     "prev_page_url": null,
	 *     "from": 1,
	 *     "to": 3,
	 *     "data": [
	 *       {
	 *         "order_id": 135,
	 *         "order_sn": "G4079525113362d",
	 *         "user_id": 59,
	 *         "order_status": 5,
	 *         "order_type": 1,
	 *         "shop_id": 0,
	 *         "order_price": "33.00",
	 *         "order_tariff_fee": "0.00",
	 *         "shipping_fee": "0.00",
	 *         "total_goods_number": 1,
	 *         "pay_price": "33.00",
	 *         "country": "中国",
	 *         "province": "北京市",
	 *         "city": "北京市",
	 *         "district": "石景山区",
	 *         "address": "西城路22号",
	 *         "consignee": "李宁",
	 *         "mobile": "17737132820",
	 *         "id_type": "1",
	 *         "id_card": "411024198903052223",
	 *         "invoice_message": null,
	 *         "order_message": "",
	 *         "deleted_at": null,
	 *         "add_time": "2016-04-07 10:14:11",
	 *         "shipping_type": "",
	 *         "shipping_num": "",
	 *         "shipping_time": null,
	 *         "shipping_message": "",
	 *         "order_pay_id": 0,
	 *         "push_tarrif": 0,
	 *         "push_tarrif_time": null,
	 *         "recive_goods_time": null,
	 *         "cancel_reason": "订单超时,系统自动取消",
	 *         "cancel_time": "2016-04-07 10:14:11",
	 *         "is_remind": 0,
	 *         "pay_push": null,
	 *         "pay_push_success_time": null,
	 *         "user_name": "admin",
	 *         "comment_status": 0,
	 *         "goods_id": 1343,
	 *         "order_goods": [
	 *           {
	 *             "goods_id": 1343,
	 *             "cat_id": 398,
	 *             "goods_sn": "33",
	 *             "goods_name": "33",
	 *             "goods_pet_name": "33",
	 *             "goods_desc": "33",
	 *             "brand_id": 74,
	 *             "goods_number": 33,
	 *             "goods_weight": "0.000",
	 *             "market_price": "33.00",
	 *             "shop_price": "33.00",
	 *             "keywords": "33",
	 *             "goods_content": "",
	 *             "goods_thumb": "",
	 *             "resource": "33",
	 *             "type": 1,
	 *             "goods_gallery_id": 0,
	 *             "is_on_sale": 1,
	 *             "add_time": 0,
	 *             "sort_order": 100,
	 *             "is_delete": 0,
	 *             "is_home_recommend": 0,
	 *             "is_cat_recommend": 0,
	 *             "shop_id": 1,
	 *             "sales_num": 0,
	 *             "deleted_at": null,
	 *             "search_keywords": "33,JJBIO,美妆个护,美容彩妆,香水/香氛,香水,香氛,女士香水,香水,女士",
	 *             "warehouse_id": 0,
	 *             "created_at": "2016-02-19 10:10:49",
	 *             "updated_at": "2016-04-07 10:14:11",
	 *             "audit_status": 1,
     *             "buy_number": 1,
     *             "order_goods_id": 474,
     *             "comment_status": 0,
	 *             "goods_bar_code": null,
     *             "mobile_goods_thumb": ""
	 *           }
	 *         ]
	 *       },
	 *       {
	 *         "order_id": 136,
	 *         "order_sn": "G4079525113862d",
	 *         "user_id": 59,
	 *         "order_status": 5,
	 *         "order_type": 0,
	 *         "shop_id": 0,
	 *         "order_price": "99.00",
	 *         "order_tariff_fee": "0.00",
	 *         "shipping_fee": "0.00",
	 *         "total_goods_number": 1,
	 *         "pay_price": "99.00",
	 *         "country": "中国",
	 *         "province": "北京市",
	 *         "city": "北京市",
	 *         "district": "石景山区",
	 *         "address": "西城路22号",
	 *         "consignee": "李宁",
	 *         "mobile": "17737132820",
	 *         "id_type": "1",
	 *         "id_card": "411024198903052223",
	 *         "invoice_message": null,
	 *         "order_message": "",
	 *         "deleted_at": null,
	 *         "add_time": "2016-04-07 10:14:11",
	 *         "shipping_type": "",
	 *         "shipping_num": "",
	 *         "shipping_time": null,
	 *         "shipping_message": "",
	 *         "order_pay_id": 0,
	 *         "push_tarrif": 0,
	 *         "push_tarrif_time": null,
	 *         "recive_goods_time": null,
	 *         "cancel_reason": "订单超时,系统自动取消",
	 *         "cancel_time": "2016-04-07 10:14:11",
	 *         "is_remind": 0,
	 *         "pay_push": null,
	 *         "pay_push_success_time": null,
	 *         "user_name": "admin",
	 *         "comment_status": 0,
	 *         "goods_id": 1346,
	 *         "order_goods": [
	 *           {
	 *             "goods_id": 1346,
	 *             "cat_id": 400,
	 *             "goods_sn": "010101",
	 *             "goods_name": "莲花香",
	 *             "goods_pet_name": "莲花香牌男士香水",
	 *             "goods_desc": "莲花香牌男士香水",
	 *             "brand_id": 79,
	 *             "goods_number": 100,
	 *             "goods_weight": "0.000",
	 *             "market_price": "199.00",
	 *             "shop_price": "99.00",
	 *             "keywords": "",
	 *             "goods_content": "",
	 *             "goods_thumb": "",
	 *             "resource": "中国",
	 *             "type": 0,
	 *             "goods_gallery_id": 0,
	 *             "is_on_sale": 1,
	 *             "add_time": 0,
	 *             "sort_order": 100,
	 *             "is_delete": 0,
	 *             "is_home_recommend": 0,
	 *             "is_cat_recommend": 0,
	 *             "shop_id": 5,
	 *             "sales_num": 0,
	 *             "deleted_at": null,
	 *             "search_keywords": ",4ASCENT,美妆个护,美容彩妆,香水/香氛,香水,香氛,中性香水,香水,中性",
	 *             "warehouse_id": 0,
	 *             "created_at": "2016-04-01 14:13:29",
	 *             "updated_at": "2016-04-07 10:14:11",
	 *             "audit_status": 1,
     *             "buy_number": 1,
     *             "order_goods_id": 475,
     *             "comment_status": 0,
	 *             "goods_bar_code": "010101",
     *             "mobile_goods_thumb": ""
	 *           }
	 *         ]
	 *       },
	 *       {
	 *         "order_id": 134,
	 *         "order_sn": "G4079520123722d",
	 *         "user_id": 59,
	 *         "order_status": 5,
	 *         "order_type": 0,
	 *         "shop_id": 0,
	 *         "order_price": "99.00",
	 *         "order_tariff_fee": "0.00",
	 *         "shipping_fee": "0.00",
	 *         "total_goods_number": 1,
	 *         "pay_price": "99.00",
	 *         "country": "中国",
	 *         "province": "北京市",
	 *         "city": "北京市",
	 *         "district": "石景山区",
	 *         "address": "西城路22号",
	 *         "consignee": "李宁",
	 *         "mobile": "17737132820",
	 *         "id_type": "1",
	 *         "id_card": "411024198903052223",
	 *         "invoice_message": null,
	 *         "order_message": "",
	 *         "deleted_at": null,
	 *         "add_time": "2016-04-07 10:13:21",
	 *         "shipping_type": "",
	 *         "shipping_num": "",
	 *         "shipping_time": null,
	 *         "shipping_message": "",
	 *         "order_pay_id": 0,
	 *         "push_tarrif": 0,
	 *         "push_tarrif_time": null,
	 *         "recive_goods_time": null,
	 *         "cancel_reason": "订单超时,系统自动取消",
	 *         "cancel_time": "2016-04-07 10:13:21",
	 *         "is_remind": 0,
	 *         "pay_push": null,
	 *         "pay_push_success_time": null,
	 *         "user_name": "admin",
	 *         "comment_status": 0,
	 *         "goods_id": 1346,
	 *         "order_goods": [
	 *           {
	 *             "goods_id": 1346,
	 *             "cat_id": 400,
	 *             "goods_sn": "010101",
	 *             "goods_name": "莲花香",
	 *             "goods_pet_name": "莲花香牌男士香水",
	 *             "goods_desc": "莲花香牌男士香水",
	 *             "brand_id": 79,
	 *             "goods_number": 100,
	 *             "goods_weight": "0.000",
	 *             "market_price": "199.00",
	 *             "shop_price": "99.00",
	 *             "keywords": "",
	 *             "goods_content": "",
	 *             "goods_thumb": "",
	 *             "resource": "中国",
	 *             "type": 0,
	 *             "goods_gallery_id": 0,
	 *             "is_on_sale": 1,
	 *             "add_time": 0,
	 *             "sort_order": 100,
	 *             "is_delete": 0,
	 *             "is_home_recommend": 0,
	 *             "is_cat_recommend": 0,
	 *             "shop_id": 5,
	 *             "sales_num": 0,
	 *             "deleted_at": null,
	 *             "search_keywords": ",4ASCENT,美妆个护,美容彩妆,香水/香氛,香水,香氛,中性香水,香水,中性",
	 *             "warehouse_id": 0,
	 *             "created_at": "2016-04-01 14:13:29",
	 *             "updated_at": "2016-04-07 10:14:11",
	 *             "audit_status": 1,
     *             "buy_number": 1,
     *             "order_goods_id": 476,
     *             "comment_status": 0,
	 *             "goods_bar_code": "010101",
     *             "mobile_goods_thumb": ""
	 *           }
	 *         ]
	 *       }
	 *     ]
	 * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //获取订单失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202
     * //获取订单失败
     * {
     *   "message": "查询失败"
     * }
	 */
	public function getOrdersByCondations(Request $request)
	{
        $user_id = $request->apiUser->get()->id;							//用户id
        $condations = $request->condations;						//查询订单的条件
        $page = empty($request->page)? 10 :$request->page;		//每页多少条数据
        $current_page = empty($request->current_page)? 1 :$request->current_page;					//当前页

		if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
		}//end if()

        try{

            $query = Order::select('order.*','user.user_name','order_goods.comment_status','order_goods.goods_id')
                ->join('user','order.user_id','=','user.id')
                ->join('order_goods','order_goods.order_id','=','order.order_id')
                ->leftJoin('refund_goods','order.order_sn','=','refund_goods.order_sn')
                ->where('order.user_id',$user_id);

            //按订单状态筛选
            if(!empty($condations['order_status'])&&$condations['order_status']!='all'){
                $query = $query->where('order.order_status',$condations['order_status']);
            }//end if()

            //按订单评论状态筛选
            if(isset($condations['comment_status'])&&$condations['comment_status']==0){
                $query = $query->where('order.order_status',4)->where('order_goods.comment_status','=',$condations['comment_status']);
            }//end if()

            //按订单时间筛选
            //$condations['date'] 1：近三个月；2：近一年；3：当年；4：去年；5：去年以前
            if(!empty($condations['date'])&&$condations['date']=='1'){
                $query = $query->where('order.add_time','>',date('Y-m-d H:i:s',strtotime('-3 month')));
            }//end if()

            if(!empty($condations['date'])&&$condations['date']=='2'){
                $query = $query->where('order.add_time','>',date('Y-m-d H:i:s',strtotime('-6 month')));
            }//end if()

            if(!empty($condations['date'])&&$condations['date']=='3'){
                $date = date("Y",strtotime(date('Y-m-d',time())));
                $query = $query->whereRaw('year(order.add_time)= '.$date);
            }//end if()

            if(!empty($condations['date'])&&$condations['date']=='4'){
                $date = date("Y",strtotime(date('Y-m-d',time())))-1;
                $query = $query->whereRaw('year(order.add_time)= '.$date);
            }//end if()

            if(!empty($condations['date'])&&$condations['date']=='5'){
                $date = date("Y",strtotime(date('Y-m-d',time())))-1;
                $query = $query->whereRaw('year(order.add_time)< '.$date);
            }//end if()

            //筛选退货订单
            if(!empty($condations['refund_goods'])){
                $query = $query->where('refund_goods.user_id', $user_id);
            }//end if()

            $query = $query->groupBy('order_goods.order_id')->orderBy('order.add_time','desc');

            $orders = $query->paginate($page,'','',$current_page);


    		foreach ($orders as $key => $value) {
    			$order_id = $value->order_id;
    			$order_goods = OrderGoods::select('goods.*','order_goods.buy_number','order_goods.id as order_goods_id','order_goods.comment_status')
    							->leftJoin('goods', 'order_goods.goods_id', '=', 'goods.goods_id')
    							->where('order_id', $order_id)
    							->get();
    			$orders[$key]['order_goods'] = $order_goods->toArray();

    		}//end foreach()

    	    $result = $orders->toArray();

    	    return response()->json($result);

        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException('查询失败');

        }//end try
	}//end func getOrdersByCondations

    /**
     * @api {post} /order/cancel 取消订单
     * @apiName Order/cancel
     * @apiGroup Order
     * @apiDescription 取消订单.
     *
     * @apiParam {Number} order_id  订单id.
     * @apiParam {String} [reason]    退货原因.
	 *
     * @apiSuccess {Object}  Object          处理的结果信息,订单取消成功返回true
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单取消成功
     * {
     *   "result": "true"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单取消失败
     * {
	 *   "message": "订单id不能为空"
	 * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单取消失败
	 * {
	 *   "message": "用户id不能为空"
	 * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202
     * //订单取消失败
	 * {
	 *   "message": "订单不存在"
	 * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 202
     * //订单取消失败
	 * {
	 *   "message": "只有待支付的订单才能取消"
	 * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 202
     * //订单取消失败
	 * {
	 *   "message": "订单中商品不存在错误"
	 * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 202
     * //订单取消失败
	 * {
	 *   "message": "订单取消失败"
	 * }
     */
    public function cancel(Request $request, OrderService $orderService){

    	$order_id = $request->order_id;
    	$reason = $request->reason;
    	$user_id = $request->apiUser->get()->id;

        if (empty($order_id)) {

            throw new ApiUserNotLoginErrorException("订单id不能为空");
        }//end if()

        if (empty($user_id)) {

            throw new ApiUserNotLoginErrorException("用户id不能为空");
        }//end if()

        try {

			$result = $orderService->cancel($order_id,$reason,$user_id);

            return response()->json(["result"=>"true"]);

        } catch (OrderServiceException $e) {

            throw new ApiInputErrorException($e->getMessage());
        }


    }//end func cancel

	/**
     * @api {post} /order/destory 删除订单
     * @apiName Order/destory
     * @apiGroup Order
     *
     * @apiParam {Number} order_id  订单id
     *
     * @apiSuccess {Object[]}  Object      处理的结果信息,删除成功返回true
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单删除成功
     * {
     *   "result": "true"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单删除失败
     * {
     *   "message": "订单id不能为空",
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单删除失败
     * {
     *   "message": "用户id不能为空",
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202
     * //订单删除失败
     * {
     *   "message": "订单不存在或已删除",
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 202
     * //订单删除失败
     * {
     *   "message": "用户不存在",
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 202
     * //订单删除失败
     * {
     *   "message": "订单id与用户不匹配",
     * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 202
     * //订单删除失败
     * {
     *   "message": "只有已取消的订单才能够删除",
     * }
	 */
    public function destory(Request $request, OrderService $orderService)
    {

        $return = ['status'=>200, 'message'=>'订单删除成功', 'data'=>''];

        $order_id = $request->order_id;
        $user_id = $request->apiUser->get()->id;

        if (empty($order_id)) {

            throw new ApiInputErrorException("订单id不能为空");
        }//end if()

        if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
        }//end if()

        try {
            $result = $orderService->destoryOrder($order_id, $user_id);

            return response()->json(["result"=>"true"]);

        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }


    }//end func destory()


    /**
     * @api {post} /order/statistics_order 统计订单数量
     * @apiName Order/statistics_order
     * @apiGroup Order
     * @apiDescription 统计订单数量.
     *
     * @apiParam {Number} [status]    订单状态：1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消；
     * @apiParam {Number} [is_comment]   是否统计评论的订单数量，不能与订单状态同时使用,统计订单状态是该值不能为空、为零
     *
     * @apiSuccess {Number}  result         订单数量
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单统计成功
     * 5
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单统计失败
     * {
     *   "message": "查询状态不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单统计失败
     * {
     *   "message": "用户id不能为空"
     * }
     */
    public function statisticsOrder(Request $request, OrderService $orderService)
    {

        $user_id = $request->apiUser->get()->id;
        $order_status = $request->status;
        $is_comment = $request->is_comment;

        if (empty($order_status) && empty($is_comment)) {

            throw new ApiInputErrorException("查询状态不能为空");
        }//end if()

        if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
        }//end if()


        if (!empty($is_comment)) {
            $count = Order::where('user_id', $user_id)->where('order_status', $order_status)->count();
        }else{
            $count = OrderGoods::where('order.order_status', $order_status)
                        ->leftJoin('comment', 'order_goods.id', '=', 'comment.order_goods_id')
                        ->leftJoin('order', 'order_goods.order_id', '=', 'order.order_id')
                        ->where('order.user_id', $user_id)
                        ->where('order.deleted_at', null)
                        ->count();
        }//end if()

        return $count;

    }//end func statistics()


    /**
     * @api {post} /order/get_order_goods 获取订单商品
     * @apiName Order/get_order_goods
     * @apiGroup Order
     * @apiDescription 获取订单商品.
     *
     * @apiParam {Number} order_id   订单id
     * @apiParam {Number} user_id   用户id
     *
     * @apiSuccess {Object}  Object         订单商品数组
     * @apiSuccess {number}  Object.id      订单商品表主键id
     * @apiSuccess {number}  Object.order_id      订单id
     * @apiSuccess {number}  Object.goods_id      商品id
     * @apiSuccess {String}  Object.goods_sn      商品编号
     * @apiSuccess {String}  Object.goods_name    商品名称
     * @apiSuccess {String}  Object.goods_pet_name      商品副标题
     * @apiSuccess {String}  Object.goods_desc          商品简介
     * @apiSuccess {number}  Object.buy_number          商品购买数量
     * @apiSuccess {number}  Object.goods_weight        商品重量
     * @apiSuccess {number}  Object.shop_price          商品价格
     * @apiSuccess {number}  Object.tariff_percent      关税比例
     * @apiSuccess {String}  Object.goods_thumb         商品缩略图
     * @apiSuccess {number}  Object.shop_id             商品所属商铺id
     * @apiSuccess {number}  Object.cat_id              分类ID
     * @apiSuccess {number}  Object.type                商品类型， 0：一般商品；1：e贸易商品
     * @apiSuccess {number}  Object.comment_status      评论状态，0：未评论；1：已评论；
     * @apiSuccess {number}  Object.brand_id            品牌ID
     * @apiSuccess {String}  Object.goods_content       商品描述
     * @apiSuccess {String}  Object.resource            商品来源
     * @apiSuccess {String}  Object.add_time            快照添加时间
     * @apiSuccess {number}  Object.order_status        订单状态：1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消；
     * @apiSuccess {String}  Object.mobile_goods_thumb  商品图片完整地址
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //获取订单商品成功
     * {
     *     {
     *       "id": 112,
     *       "order_id": 137,
     *       "goods_id": 1342,
     *       "goods_sn": "aa",
     *       "goods_name": "净白霜",
     *       "goods_pet_name": "aa",
     *       "goods_desc": "aa",
     *       "buy_number": 1,
     *       "goods_weight": "0.000",
     *       "shop_price": "22.00",
     *       "tariff_percent": 50,
     *       "goods_thumb": "",
     *       "shop_id": 1,
     *       "cat_id": 393,
     *       "type": 1,
     *       "comment_status": 0,
     *       "brand_id": 77,
     *       "goods_content": "",
     *       "resource": "aa",
     *       "add_time": 1460079208,
     *       "order_status": 1,
     *       "mobile_goods_thumb": ""
     *     },
     *     {
     *       "id": 113,
     *       "order_id": 137,
     *       "goods_id": 1343,
     *       "goods_sn": "33",
     *       "goods_name": "兰蔻",
     *       "goods_pet_name": "33",
     *       "goods_desc": "33",
     *       "buy_number": 2,
     *       "goods_weight": "0.000",
     *       "shop_price": "33.00",
     *       "tariff_percent": 50,
     *       "goods_thumb": "7635ac27ee5da6f8b6358c5a2c2188ae",
     *       "shop_id": 1,
     *       "cat_id": 398,
     *       "type": 1,
     *       "comment_status": 0,
     *       "brand_id": 74,
     *       "goods_content": "",
     *       "resource": "33",
     *       "add_time": 1460079208,
     *       "order_status": 1,
     *       "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/75053874b1a36e0ee118ba73d0a26947.png"
     *     }
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单统计失败
     * {
     *   "message": "订单id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单统计失败
     * {
     *   "message": "用户id不能为空"
     * }
     */
    public function getOrderGoods(Request $request, OrderService $orderService)
    {

        $order_id = $request->order_id;
        $user_id = $request->apiUser->get()->id;

        if (empty($order_id)) {

            throw new ApiInputErrorException("订单id不能为空");
        }//end if()

        if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
        }//end if()

        try {

            $orderGoods = OrderGoods::select('order_goods.*','order.order_status')
                ->join('order','order.order_id','=','order_goods.order_id')
                ->where('order.order_id',$order_id)
                ->where('order.user_id',$user_id)
                ->get();

            if (empty($orderGoods->count())) {
                $return['message'] = '查询完毕，没有查询到该订单的商品';
            }//end if()

            return response()->json($orderGoods->toArray());

        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }


    }//end func getOrderGoods

    /**
     * @api {post} /order/statistics_order_goods 统计订单商品总数量
     * @apiName Order/statistics_order_goods
     * @apiGroup Order
     * @apiDescription 统计订单商品总数量.
     *
     * @apiParam {Number} order_id   订单id
     * @apiParam {Number} user_id    用户id
     *
     * @apiSuccess {Number}  Object           订单商品总数量统计结果
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //统计订单商品总数量成功
     *   5
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 406
     * //订单统计失败
     * {
     *   "message": "订单id不能为空"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 406
     * //订单统计失败
     * {
     *   "message": "用户id不能为空"
     * }
     *
     */
    public function statisticsOrderGoods(Request $request, OrderService $orderService){

        $order_id = $request->order_id;
        $user_id = $request->apiUser->get()->id;

        if (empty($order_id)) {

            throw new ApiInputErrorException("订单id不能为空");
        }//end if()

        if (empty($user_id)) {

            throw new ApiInputErrorException("用户id不能为空");
        }//end if()

        try {

            $orderGoods = OrderGoods::select('order_goods.*','order.order_status')
                ->join('order','order.order_id','=','order_goods.order_id')
                ->where('order.order_id',$order_id)
                ->get();
            $GoodsNumArray = array();
            $orderGoodsNum=0;
            foreach($orderGoods as $key=>$value){
                $GoodsNumArray[$key] = $value->buy_number;
            }//end foreach()

            for($i=0;$i<count($GoodsNumArray);$i++){
                $orderGoodsNum = $orderGoodsNum + $GoodsNumArray[$i];
            }//end for()

            return $orderGoodsNum;
        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException("统计异常");
        }


    }//end func statisticsOrderGoods

    /**
     * @api {post} /order/get_orders_shop 获取订单所属商铺（现在拆单按照商品类型分为一般商品订单和e贸易订单，这个不必要）
     * @apiName Order/get_orders_shop
     * @apiGroup Order
     * @apiDescription 获取商铺.
     *
     * @apiParam {Number} order_id   订单id
     *
     * @apiSuccess {Object[]}  result         返回信息对象
     * @apiSuccess {Number}  Object.order_id      订单id
     * @apiSuccess {String}  Object.order_sn      订单编号
     * @apiSuccess {Number}  Object.user_id       用户id
     * @apiSuccess {Number}  Object.order_status      订单状态：1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消；
     * @apiSuccess {number}  Object.order_type    订单类型，0：一般商品；1：e贸易商品
     * @apiSuccess {number}  Object.shop_id         店铺id
     * @apiSuccess {number}  Object.order_price         订单价格
     * @apiSuccess {number}  Object.order_tariff_fee         订单关税价格
     * @apiSuccess {number}  Object.shipping_fee         邮费
     * @apiSuccess {number}  Object.total_goods_number         商品总数
     * @apiSuccess {number}  Object.pay_price         支付价格
     * @apiSuccess {String}  Object.country         国家
     * @apiSuccess {String}  Object.province        省份
     * @apiSuccess {String}  Object.city            城市
     * @apiSuccess {String}  Object.district        区
     * @apiSuccess {String}  Object.address         详细地地址
     * @apiSuccess {String}  Object.consignee       收货人
     * @apiSuccess {String}  Object.id_type          证件类型：默认身份证（1：身份证）
     * @apiSuccess {String}  Object.id_card         身份证号码
     * @apiSuccess {String}  Object.invoice_message         用户发票信息
     * @apiSuccess {String}  Object.order_message         订单备注信息
     * @apiSuccess {String}  Object.deleted_at         删除时间
     * @apiSuccess {String}  Object.add_time         下单时间
     * @apiSuccess {String}  Object.shipping_type         配送方式
     * @apiSuccess {String}  Object.shipping_num         运单号
     * @apiSuccess {String}  Object.shipping_time         发货时间
     * @apiSuccess {String}  Object.shipping_message         用户自定义发货信息
     * @apiSuccess {number}  Object.order_pay_id         支付ID 关联order_pay 主键
     * @apiSuccess {number}  Object.push_tarrif         是否已经推关，0：未推关；1：已推关
     * @apiSuccess {String}  Object.push_tarrif_time         推关时间
     * @apiSuccess {String}  Object.recive_goods_time         收货时间
     * @apiSuccess {String}  Object.cancel_reason         取消订单原因
     * @apiSuccess {String}  Object.cancel_time         取消订单时间
     * @apiSuccess {number}  Object.is_remind         是否提醒发货：0：不提醒；1：提醒
     * @apiSuccess {number}  Object.pay_push         支付推关结果：1、成功；2、失败
     * @apiSuccess {number}  Object.pay_push_success_time         支付推关成功的时间
     * @apiSuccess {number}  Object.id                店铺id
     * @apiSuccess {String}  Object.shop_name         店铺名称
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //获取商铺信息成功
     * {
     *     "order_id": 75,
     *     "order_sn": "G3109742233427d",
     *     "user_id": 59,
     *     "order_status": 5,
     *     "order_type": 0,
     *     "shop_id": 4,
     *     "order_price": "999.00",
     *     "order_tariff_fee": "0.00",
     *     "shipping_fee": "0.00",
     *     "total_goods_number": 1,
     *     "pay_price": "0.00",
     *     "country": "中国",
     *     "province": "河南省",
     *     "city": "郑州市",
     *     "district": "中原区",
     *     "address": "中原中路82号",
     *     "consignee": "zzss",
     *     "mobile": "15890121887",
     *     "id_type": "1",
     *     "id_card": "411521199212208346",
     *     "invoice_message": null,
     *     "order_message": "",
     *     "deleted_at": null,
     *     "add_time": "2016-03-10 16:10:22",
     *     "shipping_type": "",
     *     "shipping_num": "",
     *     "shipping_time": null,
     *     "shipping_message": "",
     *     "order_pay_id": 0,
     *     "push_tarrif": 0,
     *     "push_tarrif_time": null,
     *     "recive_goods_time": null,
     *     "cancel_reason": "订单超时,系统自动取消",
     *     "cancel_time": "2016-03-10 16:10:23",
     *     "is_remind": 0,
     *     "pay_push": null,
     *     "pay_push_success_time": null,
     *     "id": 4,
     *     "shop_name": "麦德龙"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单统计失败
     * {
     *   "message": "订单id不能为空"
     * }
     */
    public function getOrdershop(Request $request, OrderService $orderService)
    {

        $order_id = $request->order_id;

        if (empty($order_id)) {

            throw new ApiInputErrorException("订单id不能为空");
        }//end if()

        $result = $orderService->getOrdershop($order_id);

        return response()->json($result);
    }

    /**
     * @api {post} /order/get_by_order_id 根据订单id查询订单信息
     * @apiName Order/get_by_order_id
     * @apiGroup Order
     * @apiDescription 根据订单id查询订单信息.
     *
     * @apiParam {Number} order_id   订单id
     *
     * @apiSuccess {Number}  Object      订单信息
     *
     * @apiSuccess {number}  Object.order_id      订单id
     * @apiSuccess {String}  Object.order_sn      订单编号
     * @apiSuccess {number}  Object.user_id       用户id
     * @apiSuccess {number}  Object.order_status  订单状态：1:待支付; 2:待发货；3:已发货；4:已完成；5:已取消；
     * @apiSuccess {number}  Object.order_type    订单类型，0：一般商品；1：e贸易商品
     * @apiSuccess {number}  Object.shop_id         店铺id
     * @apiSuccess {number}  Object.order_price         订单价格
     * @apiSuccess {number}  Object.order_tariff_fee         订单关税价格
     * @apiSuccess {number}  Object.shipping_fee         邮费
     * @apiSuccess {number}  Object.coupon_price         优惠券优惠的价格
     * @apiSuccess {number}  Object.discount_price       限时折扣的价格
     * @apiSuccess {number}  Object.total_goods_number         商品总数
     * @apiSuccess {number}  Object.pay_price         支付价格
     * @apiSuccess {String}  Object.country         国家
     * @apiSuccess {String}  Object.province        省份
     * @apiSuccess {String}  Object.city            城市
     * @apiSuccess {String}  Object.district        区
     * @apiSuccess {String}  Object.address         详细地地址
     * @apiSuccess {String}  Object.consignee       收货人
     * @apiSuccess {String}  Object.mobile          收货人电话
     * @apiSuccess {String}  Object.id_type          证件类型：默认身份证（1：身份证）
     * @apiSuccess {String}  Object.id_card         身份证号码
     * @apiSuccess {String}  Object.invoice_message         用户发票信息
     * @apiSuccess {String}  Object.order_message         订单备注信息
     * @apiSuccess {String}  Object.deleted_at         删除时间
     * @apiSuccess {String}  Object.add_time         下单时间
     * @apiSuccess {String}  Object.shipping_type         配送方式
     * @apiSuccess {String}  Object.shipping_num         运单号
     * @apiSuccess {String}  Object.shipping_time         发货时间
     * @apiSuccess {String}  Object.shipping_message         用户自定义发货信息
     * @apiSuccess {number}  Object.order_pay_id         支付ID 关联order_pay 主键
     * @apiSuccess {number}  Object.push_tarrif         是否已经推关，0：未推关；1：已推关
     * @apiSuccess {String}  Object.push_tarrif_time         推关时间
     * @apiSuccess {String}  Object.recive_goods_time         收货时间
     * @apiSuccess {String}  Object.cancel_reason         取消订单原因
     * @apiSuccess {String}  Object.cancel_time         取消订单时间
     * @apiSuccess {number}  Object.is_remind         是否提醒发货：0：不提醒；1：提醒
     * @apiSuccess {number}  Object.pay_push         支付推关结果：1、成功；2、失败
     * @apiSuccess {number}  Object.pay_push_success_time         支付推关成功的时间
     * @apiSuccess {Object[]}  Object.orderGoods         订单商品信息
     * @apiSuccess {number}  Object.order_goods.id         订单商品表主键id
     * @apiSuccess {number}  Object.order_goods.order_id         订单id
     * @apiSuccess {Number}  Object.order_goods.goods_id                商品id
     * @apiSuccess {String}  Object.order_goods.goods_sn                商品编号
     * @apiSuccess {String}  Object.order_goods.goods_name              商品名称
     * @apiSuccess {String}  Object.order_goods.goods_pet_name          商品副标题
     * @apiSuccess {String}  Object.order_goods.goods_desc              商品简介
     * @apiSuccess {Number}  Object.order_goods.buy_number              商品购买数量
     * @apiSuccess {Number}  Object.order_goods.goods_weight            商品重量
     * @apiSuccess {Number}  Object.order_goods.shop_price              商品商城价格
     * @apiSuccess {Number}  Object.order_goods.tariff_percent          关税比例
     * @apiSuccess {String}  Object.order_goods.goods_thumb             商品缩略图
     * @apiSuccess {Number}  Object.order_goods.shop_id                 店铺id
     * @apiSuccess {Number}  Object.order_goods.cat_id                  分类id
     * @apiSuccess {Number}  Object.order_goods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.order_goods.comment_status          评论状态，0：未评论；1：已评论；
     * @apiSuccess {Number}  Object.order_goods.brand_id          品牌id
     * @apiSuccess {Number}  Object.order_goods.goods_content           商品内容
     * @apiSuccess {Number}  Object.order_goods.resource           商品来源
     * @apiSuccess {Number}  Object.order_goods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.order_goods.goods_spec
     * @apiSuccess {Number}  Object.order_goods.price                   备案价格
     * @apiSuccess {Number}  Object.order_goods.ieType                  进出口标志 I-进口  E-出口
     * @apiSuccess {Number}  Object.order_goods.stockFlag               集货/备货 1-集货 2-备货
     * @apiSuccess {Number}  Object.order_goods.batchNumbers            批次号
     * @apiSuccess {Number}  Object.order_goods.tradeCountryCiq         贸易国别（检） 进口是发货人国家的国检编码，出口是收货人国家的国检编码（参考数据字典国检的国家代码）【配置】
     * @apiSuccess {Number}  Object.order_goods.tradeCountryCus         贸易国别（关） 进口是发货人国家的海关编码，出口是收货人国家的海关编码（参考数据字典海关的国家代码）【配置】
     * @apiSuccess {Number}  Object.order_goods.packageTypeCiq          包装种类（检）
     * @apiSuccess {Number}  Object.order_goods.packageTypeCus          包装种类（关）（参考数据字典的海关包装种类代码）
     * @apiSuccess {Number}  Object.order_goods.currencyCiq             币制（检）配置
     * @apiSuccess {Number}  Object.order_goods.currencyCus             币制（关）配置
     * @apiSuccess {Number}  Object.order_goods.unitCiq                 计量单位（关）
     * @apiSuccess {Number}  Object.order_goods.unitCus                 计量单位（关）
     * @apiSuccess {Number}  Object.order_goods.originCountryCiq        原产国（关）【配置】
     * @apiSuccess {Number}  Object.order_goods.originCountryCus        原产国（关）【配置】
     * @apiSuccess {Number}  Object.order_goods.weight                  毛重(KG)
     * @apiSuccess {Number}  Object.order_goods.transportationWay       运输方式（检）【配置】
     * @apiSuccess {Number}  Object.order_goods.shipCode                运输工具（检）【配置】
     * @apiSuccess {Number}  Object.order_goods.checkStatus             审核状态：0：未审核；1：已审核
     * @apiSuccess {String}  Object.order_goods.mobile_goods_thumb      商品图片完整地址
     * @apiSuccess {String}  Object.shipping_name                       快递名称
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单查询成功
     * {
     *   "order_id": 137,
     *   "order_sn": "G4087920804837d",
     *   "user_id": 59,
     *   "order_status": 1,
     *   "order_type": 1,
     *   "shop_id": 1,
     *   "order_price": "88.00",
     *   "order_tariff_fee": "0.00",
     *   "shipping_fee": "0.00",
     *   "coupon_price": "0.00",
     *   "discount_price": "0.00",
     *   "total_goods_number": 3,
     *   "pay_price": "88.00",
     *   "country": "中国",
     *   "province": "北京市",
     *   "city": "北京市",
     *   "district": "石景山区",
     *   "address": "西城路22号",
     *   "consignee": "李宁",
     *   "mobile": "17737132820",
     *   "id_type": "1",
     *   "id_card": "411024198903052223",
     *   "invoice_message": null,
     *   "order_message": "",
     *   "deleted_at": null,
     *   "add_time": "2016-04-08 09:33:28",
     *   "shipping_type": "",
     *   "shipping_num": "",
     *   "shipping_time": null,
     *   "shipping_message": "",
     *   "order_pay_id": 0,
     *   "push_tarrif": 0,
     *   "push_tarrif_time": null,
     *   "recive_goods_time": null,
     *   "cancel_reason": "订单超时,系统自动取消",
     *   "cancel_time": "2016-04-08 09:33:28",
     *   "is_remind": 0,
     *   "pay_push": null,
     *   "pay_push_success_time": null,
     *   "id": 112,
     *   "goods_id": 1342,
     *   "goods_sn": "aa",
     *   "goods_name": "净白霜",
     *   "goods_pet_name": "aa",
     *   "goods_desc": "aa",
     *   "buy_number": 1,
     *   "goods_weight": "0.000",
     *   "shop_price": "22.00",
     *   "tariff_percent": 50,
     *   "goods_thumb": "",
     *   "cat_id": 393,
     *   "type": 1,
     *   "comment_status": 0,
     *   "brand_id": 77,
     *   "goods_content": "",
     *   "resource": "aa",
     *   "orderGoods": [
     *     {
     *       "id": 112,
     *       "order_id": 137,
     *       "goods_id": 1342,
     *       "goods_sn": "aa",
     *       "goods_name": "净白霜",
     *       "goods_pet_name": "aa",
     *       "goods_desc": "aa",
     *       "buy_number": 1,
     *       "goods_weight": "0.000",
     *       "shop_price": "22.00",
     *       "tariff_percent": 50,
     *       "goods_thumb": "",
     *       "shop_id": 1,
     *       "cat_id": 393,
     *       "type": 1,
     *       "comment_status": 0,
     *       "brand_id": 77,
     *       "goods_content": "",
     *       "resource": "aa",
     *       "add_time": 1460079208,
     *       "goods_spec": null,
     *       "price": null,
     *       "ieType": null,
     *       "stockFlag": null,
     *       "batchNumbers": null,
     *       "tradeCountryCiq": null,
     *       "tradeCountryCus": null,
     *       "packageTypeCiq": null,
     *       "packageTypeCus": null,
     *       "currencyCiq": null,
     *       "currencyCus": null,
     *       "unitCiq": null,
     *       "unitCus": null,
     *       "originCountryCiq": null,
     *       "originCountryCus": null,
     *       "weight": null,
     *       "transportationWay": null,
     *       "shipCode": null,
     *       "checkStatus": null,
     *       "mobile_goods_thumb": ""
     *     },
     *     {
     *       "id": 113,
     *       "order_id": 137,
     *       "goods_id": 1343,
     *       "goods_sn": "33",
     *       "goods_name": "兰蔻",
     *       "goods_pet_name": "33",
     *       "goods_desc": "33",
     *       "buy_number": 2,
     *       "goods_weight": "0.000",
     *       "shop_price": "33.00",
     *       "tariff_percent": 50,
     *       "goods_thumb": "7635ac27ee5da6f8b6358c5a2c2188ae",
     *       "shop_id": 1,
     *       "cat_id": 398,
     *       "type": 1,
     *       "comment_status": 0,
     *       "brand_id": 74,
     *       "goods_content": "",
     *       "resource": "33",
     *       "add_time": 1460079208,
     *       "goods_spec": null,
     *       "price": null,
     *       "ieType": null,
     *       "stockFlag": null,
     *       "batchNumbers": null,
     *       "tradeCountryCiq": null,
     *       "tradeCountryCus": null,
     *       "packageTypeCiq": null,
     *       "packageTypeCus": null,
     *       "currencyCiq": null,
     *       "currencyCus": null,
     *       "unitCiq": null,
     *       "unitCus": null,
     *       "originCountryCiq": null,
     *       "originCountryCus": null,
     *       "weight": null,
     *       "transportationWay": null,
     *       "shipCode": null,
     *       "checkStatus": null,
     *       "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/8a75e0739bad792a326d8153dc6306fe.jpg"
     *     }
     *   ],
     *   "shipping_name": ""
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单查询失败
     * {
     *   "message": "订单id不能为空",
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202
     * //订单查询失败
     * {
     *   "message": "订单不存在"
     * }
     */
    public function getByOrderId(Request $request, OrderService $orderService)
    {

        $order_id = $request->order_id;

        if (empty($order_id)) {

            throw new ApiInputErrorException("订单id不能为空");
        }//end if()


        try {

            $result = $orderService->getByOrderId($order_id);

            if ($result) {

                switch ($result->shipping_type) {

                    case 'eto':

                        $shipping_name = '申通快递';

                        break;

                    case 'yto':

                        $shipping_name = '圆通快递';

                        break;
                    default:
                        $shipping_name = '';

                        break;
                }

                $result->shipping_name = $shipping_name;
            }

            return response()->json($result);
        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }


    }//end func getByOrderId

    /**
     * @api {post} /order/recive_goods 确认收货
     * @apiName Order/recive_goods
     * @apiGroup Order
     * @apiDescription 确认收货.
     *
     * @apiParam {Number} order_id   订单id
     *
     * @apiSuccess {Object[]}  return         返回信息对象
     * @apiSuccess {Number}  Object.status    处理的状态
     * @apiSuccess {String}  Object.message   处理的结果信息
     * @apiSuccess {Number}  Object.data      商铺信息
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单查询成功
     * {
     *   "result": "true"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单查询失败
     * {
     *   "message": "订单id不能为空",
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202
     * //订单查询失败
     * {
     *   "message": "订单不存在",
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202
     * //订单查询失败
     * {
     *   "message": "订单不是已发货状态",
     * }
     */
    public function reciveGoods(Request $request, OrderService $orderService)
    {

        $order_id = $request->order_id;

        if (empty($order_id)) {

            throw new ApiInputErrorException("订单id不能为空");
        }//end if()

        try {

            $result = $orderService->reciveGoods($order_id);

            return response()->json(["result"=>"true"]);

        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }



    }//end func shipping

    /**
     * @api {post} /order/order_pay_back 支付成功后回调
     * @apiName Order/order_pay_back
     * @apiGroup Order
     * @apiDescription 支付成功后回调.
     *
     * @apiParam {String} order_sn   订单编号
     * @apiParam {String} pay_type   支付类型
     * @apiParam {Number} pay_num    支付流水号
     * @apiParam {String} pay_time   支付时间
     *
     * @apiSuccess {Number}  order_id         返回订单id
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单支付回调成功
     * {
     *   "order_id": 306
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单支付回调失败
     * {
     *   "message": "订单编号不能为空",
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单支付回调失败
     * {
     *   "message": "支付类型不能为空",
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //订单支付回调失败
     * {
     *   "message": "支付流水号不能为空",
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 400
     * //订单支付回调失败
     * {
     *   "message": "支付时间不能为空",
     * }
     *
     */
    public function orderPayBack(Request $request, OrderService $orderService)
    {
        $order_sn = $request->order_sn;     //订单编号
        $pay_type = $request->pay_type;     //支付宝：alipay；通联支付allinpay
        $pay_num = $request->pay_num;       //支付流水号
        $pay_time = $request->pay_time;     //支付时间

        if (empty($order_sn)) {

            throw new ApiInputErrorException("订单编号不能为空");
        }//end if()

        if (empty($pay_type)) {

            throw new ApiInputErrorException("支付类型不能为空");
        }//end if()

        if (empty($pay_num)) {

            throw new ApiInputErrorException("支付流水号不能为空");
        }//end if()

        if (empty($pay_time)) {

            throw new ApiInputErrorException("支付时间不能为空");
        }//end if()

        try {

            $order_id = $orderService->orderPayBack($order_sn,$pay_type,$pay_num,$pay_time);
            return Response()->json(['order_id'=>$order_id]);
        } catch (OrderServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }

    }//end func orderPayBack

    /**
     * @api {post} /order/get_goods_archive 获取订单快照
     * @apiName Order/get_goods_archive
     * @apiGroup Order
     * @apiDescription 获取订单快照.
     *
     * @apiParam {Number} order_sn   订单编号
     * @apiParam {Number} goods_id   订单商品id
     * @apiParam {String} mobile_goods_thumb   商品图片完整地址
     *
     * @apiSuccess {Object}  result         返回订单商品快照信息
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //订单快照获取成功
     * {
     *   "id": 168,
     *   "order_id": 189,
     *   "goods_id": 1347,
     *   "goods_sn": "KS100028",
     *   "goods_name": "香奈儿",
     *   "goods_pet_name": "女士香水香奈儿",
     *   "goods_desc": "女士香水香奈儿",
     *   "buy_number": 1,
     *   "goods_weight": "0.000",
     *   "shop_price": "100.01",
     *   "tariff_percent": 50,
     *   "goods_thumb": "8a75e0739bad792a326d8153dc6306fe",
     *   "shop_id": 5,
     *   "cat_id": 398,
     *   "type": 1,
     *   "comment_status": 0,
     *   "brand_id": 189,
     *   "goods_content": "",
     *   "resource": "法国",
     *   "add_time": 1462267937,
     *   "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/75053874b1a36e0ee118ba73d0a26947.png"
     * }
     *
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //订单快照获取失败
     * {
     *   "message": "订单编号不能为空",
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //订单快照获取失败
     * {
     *   "message": "订单商品id不能为空",
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //订单快照获取失败
     * {
     *   "message": "用户id不能为空",
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 202
     * //订单快照获取失败
     * {
     *   "message": "用户订单不存在",
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 202
     * //订单快照获取失败
     * {
     *   "message": "订单商品快照查询失败",
     * }
     */
    public function getGoodsArchive(Request $request)
    {
        $order_sn = $request->order_sn;     //订单编号
        $goods_id = $request->goods_id;     //订单商品id
        $user_id = $request->apiUser->get()->id;       //用户id

        if (empty($order_sn)) {
            throw new ApiInputErrorException('订单编号不能为空');
        }//end if()

        if (empty($goods_id)) {
            throw new ApiInputErrorException('订单商品id不能为空');
        }//end if()

        if (empty($user_id)) {
            throw new ApiInputErrorException('用户id不能为空');
        }//end if()

        $order_id = Order::where('order_sn', $order_sn)->where('user_id', $user_id)->pluck('order_id');

        if ($order_id) {

            $order_goods = OrderGoods::where('order_id', $order_id)->where('goods_id', $goods_id)->first();

            if (empty($order_goods)) {

                throw new ApiDoneErrorException('订单商品快照查询失败');
            }//end if()

            return $order_goods;

        }else{

            throw new ApiDoneErrorException('用户订单不存在');
        }//end if()

    }//end func getGoodsArchive

}